﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryProject : FormTemplet
    {
        public FormQueryProject()
        {
            this.InitializeComponent();
        }

        private void FormQueryCustomer_Load(object sender, EventArgs e)
        {
            this.cbCondition.SelectedIndex = 0;
            if (!LoginInfo.Role[PowerName.Project_Delete])
            {
                this.btnDelete.Enabled = false;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string text = "SELECT pj.PKID AS 序号,c.CompanyName AS 客户,pj.ProjectName AS 计划,pj.Linkman AS 联系人,pj.LinkmanPhone AS 联系电话,pj.ProjectAddress AS 地址,pj.Remark AS 备注 FROM Project AS pj INNER JOIN Customer AS c ON pj.CustomerID=c.PKID";
            if (this.cbCondition.SelectedIndex == 0)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE pj.ProjectName LIKE '%" + this.txtCondition.Text.Trim().Replace(" ", "%") + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(pj.ProjectName) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
            }
            if (this.cbCondition.SelectedIndex == 1)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE pj.Linkman LIKE '%" + this.txtCondition.Text.Trim().Replace(" ", "%") + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(pj.Linkman) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
            }
            if (this.cbCondition.SelectedIndex == 2)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE pj.ProjectAddress LIKE '%" + this.txtCondition.Text.Trim().Replace(" ", "%") + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(pj.ProjectAddress) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
            }
            text += " ORDER BY pj.PKID DESC";
            Projects projects = new Projects();
            DataTable dataTable = null;
            try
            {
                dataTable = projects.GetProjects(text);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n获取数据时出现错误，请从试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            projects.Dispose();
            if (dataTable != null)
            {
                this.dgProjects.DataSource = null;
                this.dgProjects.DataSource = dataTable;
                this.dgProjects.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Project_Update])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                try
                {
                    string text = this.dgProjects.Rows[0].Cells[0].Value.ToString();
                }
                catch (Exception)
                {
                    MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                int rowIndex = this.dgProjects.CurrentCell.RowIndex;
                new FormUpdateProject(Convert.ToInt32(this.dgProjects.Rows[rowIndex].Cells[0].Value))
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnShowOrdersForProject_Click(object sender, EventArgs e)
        {
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Project_Delete])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.dgProjects.Rows.Count < 1)
            {
                MessageBox.Show("没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (MessageBox.Show("你确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.Cancel)
            {
                Projects projects = new Projects();
                bool flag = false;
                try
                {
                    flag = projects.Delete(Convert.ToInt32(this.dgProjects.Rows[this.dgProjects.CurrentRow.Index].Cells[0].Value));
                }
                catch (SqlException ex)
                {
                    int number = ex.Number;
                    if (number != 547)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show("操作失败，在计划下面没有包含订单和发货单时才能删除计划！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                projects.Dispose();
                if (flag)
                {
                    MessageBox.Show("操作成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.dgProjects.Rows.Remove(this.dgProjects.CurrentRow);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
